package help_center

import (
	"errors"
	"hft/Database/admin"
	"hft/Http/Services/front/help_center/help_center_model"
	"hft/e"
	"hft/global"
)

type HelpCenter struct{}

// 获取帮助中心
func (*HelpCenter) List() (data []*help_center_model.FirstClass) {
	var list = make([]*admin.AboutUsCategory, 0)
	global.OC_DB.Preload("SmallCategories").Find(&list)
	var resList = make([]*help_center_model.FirstClass, 0)
	for _, v := range list {
		var item = help_center_model.FirstClass{
			ID:    v.ID,
			Name:  v.Name,
			Level: 1,
		}
		for _, j := range v.SmallCategories {
			var item2 = &help_center_model.SecondClass{
				ID:    j.ID,
				Name:  j.Name,
				Level: 2,
			}
			item.Children = append(item.Children, *item2)
		}
		resList = append(resList, &item)
	}
	return resList
}

// 获取内容
func (*HelpCenter) Info(id int) (data *help_center_model.HelpInfo, err error) {
	var info *admin.SmallCategory
	if err = global.OC_DB.Preload("Articles").First(&info, id).Error; err != nil {
		return nil, errors.New(e.ArcNotExist)
	} else {
		var nowInfo = &help_center_model.HelpInfo{ID: int(info.ID), Title: info.Name, Introduce: info.Introduce}
		for _, v := range info.Articles {
			nowInfo.Children = append(nowInfo.Children, &help_center_model.Cont{
				ID:    int(v.ID),
				Title: v.Title,
				Cont:  v.Cont,
			})
		}
		return nowInfo, nil
	}
}

var HelpCenterService = new(HelpCenter)
